import com.sun.xml.internal.ws.policy.AssertionSet;

import java.util.*;

public class work2 {
    public static List<Integer> findAnagrams(String s, String p) {
        HashMap<Character,Integer>map=new HashMap<>();
        List<Integer> list=new ArrayList<>();
        int left=0;
        int right=p.length()-1;
        HashMap<Character,Integer>map1=new HashMap<>();
        for (int i = 0; i <p.length() ; i++) {
            char temp=p.charAt(i);
            map1.put(temp,map1.getOrDefault(temp,0)+1);
        }
        for (int i=left;i<=right;i++){
            char temp=s.charAt(i);
            map.put(temp,map.getOrDefault(temp,0)+1);
        }
        while (right<s.length()){
            if (map.equals(map1)){
                list.add(left);
            }
            map.put(s.charAt(left),map.getOrDefault(s.charAt(left),0)-1);
            if(map.get(s.charAt(left))==0)map.remove(s.charAt(left));
            right++;
            if (right<s.length())map.put(s.charAt(right),map.getOrDefault(s.charAt(right),0)+1);
            left++;

        }
        return list;
    }

    public static void main(String[] args) {
        String s="cbaebabacd";
        String p="abc";
        System.out.println(findAnagrams(s,p));
    }
}
